package vn.edu.saigontech.ncms.service.impl;

import java.util.Collection;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import vn.edu.saigontech.ncms.model.englishtest.Candidate;
import vn.edu.saigontech.ncms.service.CandidateService;
import vn.edu.saigontech.ncms.util.Checker;

@SuppressWarnings("unchecked")
public class CandidateServiceImpl extends HibernateGenericService<Candidate, Long>
		implements CandidateService {

	@Override
	public Collection<Candidate> find(Candidate candidate) {
		Criteria studentCriteria = this.createCriteria();
		if (!Checker.isEmpty(candidate.getFirstName())) {
			studentCriteria.add(Restrictions.ilike("firstName", candidate
					.getFirstName(), MatchMode.ANYWHERE));
		}
		if (!Checker.isEmpty(candidate.getLastName())) {
			studentCriteria.add(Restrictions.ilike("lastName", candidate
					.getLastName(), MatchMode.ANYWHERE));
		}
		if (!Checker.isEmpty(candidate.getNickName())) {
			studentCriteria.add(Restrictions.ilike("nickName", candidate
					.getNickName(), MatchMode.ANYWHERE));
		}
		if (!Checker.isNull(candidate.getGender())) {
			studentCriteria.add(Restrictions.eq("gender", candidate.getGender()));
		}
		if (!Checker.isNull(candidate.getDateOfBirth())) {
			studentCriteria.add(Restrictions.eq("dateOfBirth", candidate
					.getDateOfBirth()));
		}
		if (!Checker.isEmpty(candidate.getPhoneNumber())) {
			studentCriteria.add(Restrictions.ilike("phoneNumber", candidate
					.getPhoneNumber(), MatchMode.ANYWHERE));
		}
		if (!Checker.isEmpty(candidate.getEmail())) {
			studentCriteria.add(Restrictions.ilike("email", candidate.getEmail(),
					MatchMode.ANYWHERE));
		}
		if (!Checker.isEmpty(candidate.getAddress())) {
			studentCriteria.add(Restrictions.ilike("address", candidate
					.getAddress(), MatchMode.ANYWHERE));
		}
		if (!Checker.isNull(candidate.getType())) {
			studentCriteria.add(Restrictions.eq("type", candidate.getType()));
		}
		if (!Checker.isNull(candidate.getConsultant())) {
			studentCriteria.createCriteria("consultant").add(
					Restrictions.eq("id", candidate.getConsultant().getId()));
		}
		studentCriteria.addOrder(Order.asc("firstName"));
		studentCriteria.addOrder(Order.asc("lastName"));
		return studentCriteria.list();
	}

	public Collection<Candidate> findRecently() {
		Criteria criteria = this.createCriteria();
		criteria.addOrder(Order.desc("id"));
		criteria.setMaxResults(5);
		return criteria.list();
	}
}